package com.fly.caihong.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.fly.caihong.R;
import com.fly.caihong.adapter.MallCategoryAdapter;
import com.fly.caihong.base.BaseActivity;
import com.fly.caihong.model.MallCategoryItem;
import com.fly.caihong.model.MallGoodsItem;
import com.fly.caihong.utils.DataHandler;
import com.fly.caihong.utils.StatusBarUtils;
import com.fly.caihong.utils.ToastUtils;
import com.fly.caihong.view.PullRefreshRecyclerView;

import java.util.ArrayList;

public class MallCategoryActivity extends BaseActivity {

    PullRefreshRecyclerView mPullRefreshLayout;
    MallCategoryAdapter mAdapter;
    MallCategoryItem mallCategoryItem;

    @Override
    protected int getLayout() {
        return R.layout.activity_mall_category;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        StatusBarUtils.setColor(this, ActivityCompat.getColor(this, R.color.theme_bg), 0);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void setStatusBarStyle() {
    }

    @Override
    protected void initViews() {
        super.initViews();
        mPullRefreshLayout = findViewById(R.id.pull_refresh_layout);
        mAdapter = new MallCategoryAdapter(new ArrayList<MallGoodsItem>());
        mallCategoryItem = DataHandler.convertJsonData("home/mall/category", MallCategoryItem.class);
        findViewById(R.id.btn_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        if (mallCategoryItem == null) {
            ToastUtils.showLong("商品分类列表数据为空");
            return;
        }
        mPullRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPullRefreshLayout.setRefreshing(false);
            }
        });
        mAdapter.addData(mallCategoryItem.getGoodsList());
        mPullRefreshLayout.setAdapter(mAdapter);
        mPullRefreshLayout.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                GoodsDetailActivity.launchActivity(MallCategoryActivity.this, ((MallGoodsItem) adapter.getItem(position)).getId());
            }
        });
    }

}
